package dao;

import model.Customer;
import model.Goods;
import model.Out;
import model.Ware;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Dao_ex_ware extends Dao{
    //获取出库单显示信息
    public static Customer select_All(String wareID)
    {
        Customer customer = new Customer();
        String sql = "select distinct * from warehouse_2 where o_no = '" + wareID + "'";

        ResultSet re = executeQuery(sql);
        try {
            if(re.next())
            {
                customer.setCusName(re.getString("cl_name"));
                customer.setCusPhone(re.getString("cl_phone"));
                customer.setCusID(re.getString("cl_no"));
                customer.setO_no(re.getString("o_no"));
                customer.setTime(re.getString("o_time"));
                customer.setCusAddress(re.getString("cl_address"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return customer;
    }


    public static List select_All_1()
    {
        List list = new ArrayList();
        String sql = "select distinct * from warehouse_2";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                Ware ware = new Ware();

                ware.setWareID(re.getString("o_no"));
                ware.setAddress(re.getString("cl_address"));
                ware.setsPhone(re.getString("cl_phone"));
                ware.setSupplierID(re.getString("cl_no"));
                ware.setTime(re.getString("o_time"));;
                ware.setSupplierName(re.getString("cl_name"));

                list.add(ware);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //获取出库单ID
    public static Vector<String> getWareID()
    {
        List list = Dao_ex_ware.select_All_1();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Ware ware = (Ware) list.get(i);
            WareID.add(ware.getWareID());
        }

        return WareID;
    }

    //出库商品明细
    public static List select_goods(String wareID)
    {
        List list = new ArrayList();
        String sql = "select * from goods_2 where o_no = '" + wareID + "'";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                Goods ware = new Goods();

                ware.setWareID(re.getString("o_no"));
                ware.setGoodsID(re.getString("c_no"));
                ware.setReal(re.getDouble("reals"));
                ware.setGoodsName(re.getString("c_name"));
                ware.setType(re.getString("cs_name"));
                ware.setPrice(re.getDouble("price"));
                ware.setNumber(re.getInt("o_number"));
                ware.setDiscount(re.getDouble("discount"));
                ware.setTotal(re.getDouble("total"));

                list.add(ware);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }


    //通过出库单编号获取修改视图所有商品
    public static List select_goods_3(String detailID)
    {
        List list = new ArrayList();
        String sql = "select * from goods_3 where o_no = '" + detailID + "'";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                Goods ware = new Goods();

                ware.setDetailID(re.getString("od_no"));
                ware.setGoodsID(re.getString("c_no"));
                ware.setReal(re.getDouble("reals"));
                ware.setGoodsName(re.getString("c_name"));
                ware.setType(re.getString("cs_name"));
                ware.setPrice(re.getDouble("price"));
                ware.setNumber(re.getInt("o_number"));
                ware.setDiscount(re.getDouble("discount"));
                ware.setTotal(re.getDouble("total"));

                list.add(ware);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //添加出库单
    public static int addOut(String o_no, String cl_no, String o_discount, String o_total, String o_real)
    {
        int i = 0;
        try {

            String sql = "insert into out_repertory(o_no, cl_no, o_discount, o_total, o_real) values('"+ o_no +"','"+ cl_no +"','"+ o_discount +"','"+ o_total +"','"+ o_real +"')";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //添加出库明细
    public static int add(String od_no, String c_no, String o_no, String o_number, String o_price, String o_realPrice, String o_time)
    {
        int i = 0;

        try {
            String sql = "insert into out_details(od_no, c_no, o_no, o_number, o_price, o_realprice, o_time) values('"+ od_no +"','"+ c_no +"','"+ o_no +"',"+ o_number +","+ o_price +"," + o_realPrice + ",'" + o_time + "')";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //删除出库明细
    public static int deleteDetail(String detailID) {
        int i = 0;
        try {
            String sql = "delete from  out_details where od_no = '" + detailID + "'";
            i = Dao.exeUpdate(sql);
            System.out.println(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;
    }

    //修改出库明细数量
    public static int updateDetail(String detailID, String number, String o_price, String o_realPrice) {
        int i = 0;
        try {
            String sql = "update out_details set o_number = " + number  + ", o_price = " + o_price + ", o_realprice = " + o_realPrice + " where od_no = '" + detailID + "'";
            i = Dao.exeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;
    }

    //删除出库单
    public static int delete_out(String o_no) {
        int i = 0;
        try {
            String sql = "delete from  out_repertory where o_no = '" + o_no + "'";
            i = Dao.exeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;
    }

    //获取所有出库单信息
    public static List select_outAll()
    {
        List list = new ArrayList();
        String sql = "select distinct * from out_repertory";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                Out out = new Out();

                out.setO_no(re.getString("o_no"));
                out.setCl_no(re.getString("cl_no"));
                out.setO_discount(re.getString("o_discount"));
                out.setO_total(re.getString("o_total"));
                out.setO_real(re.getString("o_real"));

                list.add(out);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //获取出库单编号
    public static Vector<String> getOutID()
    {
        List list = Dao_ex_ware.select_outAll();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Out out = (Out) list.get(i);
            WareID.add(out.getO_no());
        }

        return WareID;
    }

}
